#include "GraphicsButton.h"
#include <QPainter>
#include <QGraphicsSceneMouseEvent>

GraphicsButton::GraphicsButton(QGraphicsItem *parent)
	: QGraphicsItem(parent)
{
	this->_isPressed = false;
}

GraphicsButton::GraphicsButton( const QPixmap &icon, const QPixmap &iconAfterPressed, QGraphicsItem *parent )
	: QGraphicsItem(parent)
{
	this->_icon = icon;
	this->_iconAfterPressed = iconAfterPressed;
	this->_isPressed = false;
}

GraphicsButton::~GraphicsButton()
{

}


void GraphicsButton::paint( QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget )
{
	Q_UNUSED(option);
	Q_UNUSED(widget);

	if ( _isPressed == false ) {
		painter->drawPixmap(0,0,_icon);
	} else if ( _isPressed == true ){
		painter->drawPixmap(0,0,_iconAfterPressed);
	}
}

QRectF GraphicsButton::boundingRect() const
{
	return _icon.rect();
}

void GraphicsButton::mousePressEvent( QGraphicsSceneMouseEvent* event)
{
	if ( event->button() == Qt::LeftButton ) {
		if ( contains( event->pos() )) {
			_isPressed = true;
		}
		update();
	}
}

void GraphicsButton::mouseMoveEvent( QGraphicsSceneMouseEvent* event)
{
	if ( event->button() == Qt::LeftButton ) {
		if ( contains( event->pos() )) {
			_isPressed = true;
		} else {
			_isPressed = false;
		}
		update();
	}
}

void GraphicsButton::mouseReleaseEvent( QGraphicsSceneMouseEvent* event )
{
	if( event->button() == Qt::LeftButton ){
		_isPressed = false;
		update();

		if ( contains( event->pos() ) ){
			emit triggered();
		}
	}
}

void GraphicsButton::setIcon( const QPixmap& icon )
{
	this->_icon = icon;
}

QPixmap& GraphicsButton::getIcon()
{
	return this->_icon;
}
